package com.itxckf.controller;


import com.itxckf.pojo.Result;
import com.itxckf.pojo.User;
import com.itxckf.service.UserService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/getUser")
    public Result getUser(@RequestBody UserQuery userQuery){
        return Result.success(userService.getUser(userQuery.getName(),userQuery.getSts()));
    };
    @PostMapping("/editUser")
    public Result editUser(@RequestBody User user){
        log.info("编辑用户，{}",user);
        userService.editUser(user);
        return Result.success("编辑成功！");
    };
    @PostMapping("/deleteUser")
    public Result deleteUser(@RequestParam Short id){
        log.info("删除用户，{}",id);
        userService.deleteUser(id);
        return Result.success("删除成功！");
    };

    /**
     * 修改用户的余额
     * @param editUserBalanceForm
     * @return
     */
    @PostMapping("/editUserBalance")
    public Result editUserBalance(@RequestBody EditUserBalanceForm editUserBalanceForm){
        log.info("修改用户的余额，{}",editUserBalanceForm);
        userService.editUserBalance(editUserBalanceForm.getId(),editUserBalanceForm.getName());
        return Result.success("修改成功！");
    };

    /**
     * 获取用户的信息
     * @param getUserInfoForm
     * @return
     */
    @PostMapping("/getUserInfo")
    public Result getUserInfo(@RequestBody GetUserInfoForm getUserInfoForm){
        log.info("获取用户的信息，{}",getUserInfoForm.getId());
        List<User> user = userService.getUserInfo(getUserInfoForm.getId());
        return Result.success(user);
    };
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class UserQuery {
    private String name;
    private String sts;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class EditUserBalanceForm {
    private String name;
    private Short id;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class GetUserInfoForm {
    private Short id;
}